﻿
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// 场景切换模块 通过配置文件 切换场景
/// </summary>
public class LevelManager : Singleton<LevelManager>
{
    public void LoadScene(string name, Action action = null)
    {
        SceneManager.LoadScene(name);
        action?.Invoke();
    }

    public void LoadSceneAsync(string name, Action action = null)
    {
        // 协程管理器 开启协程
        MonoCoroutine.Instance.StartCoroutine(ReallyLoadSceneAsync(name, action));
    }

    private IEnumerator ReallyLoadSceneAsync(string name, Action action = null)
    {
        AsyncOperation ao = SceneManager.LoadSceneAsync(name);

        while (!ao.isDone)
        {
            // 事件管理器 更新进度
            EventManager.Instance.EventTrigger("ProgressUpdate", ao.progress);
            yield return ao.progress;
        }

        action?.Invoke();
    }

}
